package com.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.service.AttrAttrgroupRelationService;
import com.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrGroupService;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;



/**
 * 属性分组
 *
 * @author lubin
 * @email lubinyx@163.com
 * @date 2021-11-02 15:15:24
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long categoryId){
        PageUtils page = attrGroupService.queryPage(params,categoryId);
        return R.ok().put("page", page);
    }

    @RequestMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R listRelation(@PathVariable Long attrgroupId){
        //PageUtils page = attrGroupService.queryPage(params);

        List<AttrEntity> list = relationService.getRelaotionAttrs(attrgroupId);

        return R.ok().put("data", list);
    }

    @RequestMapping("/attr/relation/delete")
    public R deleteRelations(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities){
        relationService.deleteRelations(relationEntities);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R listNoAttr(@RequestParam Map<String, Object> params,@PathVariable Long attrgroupId){
        PageUtils page = attrGroupService.queryNoAttrPage(params,attrgroupId);
        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    @PostMapping("/attr/relation")
    public R saveRelations(@RequestBody AttrAttrgroupRelationEntity[] relations){
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = Arrays.asList(relations);
        relationService.saveBatch(attrAttrgroupRelationEntities);
        return R.ok();
    }


    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable Long catelogId){

        List<AttrGroupVo> list = attrGroupService.getAttrGroupWithAttr(catelogId);

        return R.ok().put("data",list);
    }

}
